﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using Tesla.Math;
using Tesla.Xna.Util;
using XFG = Microsoft.Xna.Framework.Graphics;
using XNA = Microsoft.Xna.Framework;

namespace Tesla.Xna.Graphics.Implementation {
    public sealed class XNATextureCubeImplementation : TextureCubeImplementation {
        private XFG.GraphicsDevice _graphicsDevice;
        private XFG.TextureCube _textureCube;

        internal XFG.TextureCube XNATextureCube {
            get {
                return _textureCube;
            }
        }

        public override int MipCount {
            get {
                return _textureCube.LevelCount;
            }
        }

        internal XNATextureCubeImplementation(XFG.GraphicsDevice device, int size, bool genMipMaps, SurfaceFormat format) 
            : base(size, format) {
            _graphicsDevice = device;

            _textureCube = new XFG.TextureCube(device, size, genMipMaps, XNAHelper.ToXNASurfaceFormat(format));
        }

        internal XNATextureCubeImplementation(XFG.GraphicsDevice device, int size, bool genMipMaps, SurfaceFormat format, DepthFormat depthFormat, int multiSampleCount, RenderTargetUsage usage) 
            : base(size, format, depthFormat, multiSampleCount, usage) {
            _graphicsDevice = device;

            _textureCube = new XFG.RenderTargetCube(device, size, genMipMaps, XNAHelper.ToXNASurfaceFormat(format), XNAHelper.ToXNADepthFormat(depthFormat), multiSampleCount, (XFG.RenderTargetUsage) usage);
        }

        ~XNATextureCubeImplementation() {
            Dispose(false);
        }

        public override void GetData<T>(CubeMapFace face, T[] data, int mipLevel, Rectangle? subimage, int startIndex, int elementCount) {
            if(subimage.HasValue) {
                XNA.Rectangle rect;
                Rectangle v = subimage.Value;
                XNAHelper.ConvertRectangle(ref v, out rect);
                _textureCube.GetData<T>(XNAHelper.ToXNACubeMapFace(face), mipLevel, rect, data, startIndex, elementCount);
            } else {
                _textureCube.GetData<T>(XNAHelper.ToXNACubeMapFace(face), mipLevel, null, data, startIndex, elementCount);
            }
        }

        public override void SetData<T>(CubeMapFace face, T[] data, int mipLevel, Rectangle? subimage, int startIndex, int elementCount) {
            if(subimage.HasValue) {
                XNA.Rectangle rect;
                Rectangle v = subimage.Value;
                XNAHelper.ConvertRectangle(ref v, out rect);                _textureCube.SetData<T>(XNAHelper.ToXNACubeMapFace(face), mipLevel, rect, data, startIndex, elementCount);
            } else {
                _textureCube.SetData<T>(XNAHelper.ToXNACubeMapFace(face), mipLevel, null, data, startIndex, elementCount);
            }
        }

        #region GraphicsResource methods

        protected override void Dispose(bool disposing) {
            if(!IsDisposed) {
                //Dispose of managed resources
                if(disposing) {
                    if(_textureCube != null) {
                        _textureCube.Dispose();
                    }
                }
            }
            base.Dispose(disposing);
        }
        #endregion
    }
}
